<template>
  <div class="recruit-list">
    <div class="list-serach">
      <div class="admin-search">
        <a-input-group compact>
          <a-select default-value="projectId" @change="handleChange">
            <a-select-option value="projectId"> 项目id </a-select-option>
            <a-select-option value="singleId"> 案例id </a-select-option>
          </a-select>
          <a-input-search
            size="default"
            v-model="adminSearchId"
            enter-button
            @search="handleAdminSearch"
          />
        </a-input-group>
      </div>
      <a-button type="primary" @click="resetSearch">重置搜索条件</a-button>
    </div>
    <a-table
      :row-selection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
      }"
      :columns="columns"
      :data-source="recruitList"
      :pagination="false"
      :rowKey="(record) => record.id"
      :scroll="{ y: 500 }"
    >
      <span slot="customTitle">招聘信息id</span>
      <span slot="pic" slot-scope="text, record">
        <img
          style="
            width: 100px;
            height: 100px;
            border-radius: 5%;
            box-shadow: 2px 2px 5px #333333;
          "
          :src="record.cover"
        />
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button
          type="primary"
          style="margin-right: 8px"
          icon="eye"
          @click="showDetail(record.id)"
          >查看</a-button
        >
        <a-button
          type="danger"
          style="margin-right: 8px"
          icon="delete"
          @click="delSingle(record.id)"
          >删除</a-button
        >
      </span>
    </a-table>
    <div class="bottom">
      <div class="button-group">
        <a-button type="danger" class="del-list-btn" @click="delAll"
          >删除选中</a-button
        >
      </div>

      <a-pagination
        class="pageTab"
        :current="current"
        :page-size-options="pageSizeOptions"
        :total="recruitNum"
        :show-total="(total) => `共 ${recruitNum} 条`"
        :page-size="limit"
        :default-current="1"
        show-size-changer
        @showSizeChange="onShowSizeChange"
        @change="pageChange"
      />
      <a-modal
        centered
        title="删除招聘信息"
        v-model="delModalVisible"
        @ok="delOk"
        @cancel="delNo"
      >
        <p>此操作不可逆！确认删除？</p>
      </a-modal>
    </div>
  </div>
</template>

<script>
import {
  getAllRecruitByProjectIdApi,
  deleteRecruitApi,
  getRecruitByIdAdminApi,
  getAllRecruitAdminApi,
} from "@/services/superAdmin";
import moment from "moment";
export default {
  data() {
    return {
      columns: [
        {
          title: "招聘信息id",
          dataIndex: "id",
          key: "recruitId",
          align: "center",
        },
        {
          title: "岗位名称",
          dataIndex: "name",
          key: "name",
          align: "center",
        },
        {
          title: "地址",
          dataIndex: "address",
          key: "address",
          align: "center",
        },
        {
          title: "项目名",
          dataIndex: "projectName",
          key: "projectName",
          align: "center",
        },
        {
          title: "创建时间",
          dataIndex: "createdAt",
          key: "createdAt",
          align: "center",
        },
        {
          title: "操作",
          key: "action",
          scopedSlots: { customRender: "action" },
          align: "center",
        },
      ],
      keyword: "",
      adminSearchId: "",
      recruitNum: 0, //招聘信息数量
      pageSizeOptions: ["5", "10", "15", "20", "25"], //每页招聘信息数选择
      current: 1, //当前页面
      limit: 5, //每页多少条
      recruitList: [],
      selectedRowKeys: [], //选择的列数
      delModalVisible: false,
      currentRecruitId: 0,
      action: "", // 删除单条还是多条
      searchType: "projectId",
      desc: "",
    };
  },
  async created() {
    if (this.$route.query.id) {
      this.adminSearchId = this.$route.query.id;
      this.searchByProId(this.limit, 0);
    } else {
      this.getAllRecruitAdmin(this.limit, 0);
    }
  },
  methods: {
    // 重置搜索
    async resetSearch() {
      this.adminSearchId = "";
      this.$message.success(`重置搜索条件成功`, 1.5);
      this.getAllRecruitAdmin(this.limit, 0);
      this.current = 1;
      this.recruitList = [];
    },
    // admin获取所有招聘列表
    async getAllRecruitAdmin(limit, offset) {
      let res = await getAllRecruitAdminApi({
        limit,
        offset,
      });
      this.recruitList = res.data.rows;
      this.recruitList.forEach((ele) => {
        ele.createdAt = moment(ele.createdAt).format("YYYY-MM-DD  HH:mm:ss");
      });
      this.recruitNum = res.data.count;
    },
    // admin搜索框选择
    handleChange(value) {
      this.searchType = value;
      this.adminSearchId = "";
    },
    // admin搜索
    handleAdminSearch() {
      switch (this.searchType) {
        case "projectId":
          this.searchByProId();
          break;
        case "singleId":
          this.getRecruitByIdAdmin();
          break;
      }
    },
    // 按项目id搜索，获取列表
    async searchByProId() {
      if (!this.adminSearchId) {
        return;
      }
      let offset = (this.current - 1) * this.limit;
      try {
        let res = await getAllRecruitByProjectIdApi({
          id: this.adminSearchId - 0,
          limit: this.limit,
          offset,
        });
        this.recruitList = [];
        this.recruitList.push(...res.data.rows);
        if (this.recruitList.length == 0) {
          this.$message.error("该项目下无案例,请重新输入", 1);
          setTimeout(() => {
            this.getAllRecruitAdmin(this.limit, 0);
            this.current = 1;
          }, 1000);
          this.adminSearchId = "";
          return;
        }
        this.recruitList.forEach((ele) => {
          ele.createdAt = moment(ele.createdAt).format("YYYY-MM-DD  HH:mm:ss");
          ele.projectName = res.data.projectName;
        });
        this.recruitNum = res.data.count;
      } catch (error) {
        this.adminSearchId = "";
        this.$message.error(`请重新输入`, 1);
      }
    },
    // admin按id搜索
    async getRecruitByIdAdmin() {
      if (!this.adminSearchId) {
        return;
      }
      try {
        let res = await getRecruitByIdAdminApi({
          id: this.adminSearchId - 0,
        });
        this.recruitList = res.data ? [res.data] : [];
        if (this.recruitList) {
          this.recruitList[0].createdAt = moment(
            this.recruitList[0].createdAt
          ).format("YYYY-MM-DD  HH:mm:ss");
        }
        this.recruitNum = this.recruitList.length;
      } catch (error) {
        this.adminSearchId = "";
        this.$message.error(`请重新输入`, 1);
        setTimeout(() => {
          this.getAllRecruitAdmin(this.limit, 0);
          this.current = 1;
        }, 1000);
      }
    },
    // 跳转招聘信息详情
    showDetail(id) {
      this.$router.push(`/recruit/recruitDetail/${id}`);
    },
    // 删除单条modal框
    delSingle(id) {
      this.currentRecruitId = id;
      this.delModalVisible = true;
      this.action = "deleteSingle";
    },
    // 删除多条modal框
    delAll() {
      this.action = "deleteAll";
      let flag = this.selectedRowKeys.length;
      if (flag == 0) {
        this.$message.error("未选中任何招聘信息", 1);
      } else {
        this.delModalVisible = true;
      }
    },
    // 确认删除
    async delOk() {
      switch (this.action) {
        case "deleteSingle": {
          let res = await this.delRecruit();
          res.code == 200 && this.$message.success(res.msg, 1);
          this.delModalVisible = false;
          break;
        }
        case "deleteAll":
          this.delAllRecruit();
          break;
      }
      this.searchId = "";

      this.getAllRecruitAdmin(this.limit, 0);
      this.current = 1;
    },
    //删除操作
    async delRecruit(id = this.currentRecruitId) {
      this.currentRecruitId = "";
      return await deleteRecruitApi({ id });
    },
    // 删除多条的操作
    delAllRecruit() {
      let delArr = this.selectedRowKeys;
      try {
        delArr.map((ele) => {
          this.delRecruit(ele);
        });
      } catch (error) {
        this.$message.error("删除失败", 1);
      }
      this.$message.success(`成功删除${delArr.length}条案例`, 1);
      this.selectedRowKeys = [];
      this.delModalVisible = false;
    },
    delNo() {
      this.delModalVisible = false;
      this.$message.info("取消删除", 1);
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys;
    },
    // 分页
    onShowSizeChange(current, pageSize) {
      this.limit = pageSize;
      this.pageChange(current);
    },
    pageChange(current) {
      this.current = current;
      let offset = (this.current - 1) * this.limit;
      if (this.adminSearchId) {
        this.searchByProId();
        return;
      }
      this.getAllRecruitAdmin(this.limit, offset);
    },
  },
};
</script>

<style lang="scss" scoped>
.add {
  margin-bottom: 10px;
}
.list-serach {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  .ant-input-search {
    width: 200px;
  }
  .admin-search {
    .ant-select {
      min-width: 80px;
      .ant-select-selection {
        display: flex;
        flex-direction: column;
      }
    }
  }
  .ant-input-group {
    display: flex;
  }
}
.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 1rem;
  .button-group {
    button {
      margin-right: 10px;
    }
  }
  .pageTab {
    text-align: right;
  }
  .del-list-btn {
    text-align: center;
  }
}
::v-deep .ant-table-body {
  background-color: white;
}
</style>
